package com.quantgroup.data.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quantgroup.data.config.FirstApolloConfig;
import com.quantgroup.data.dao.RyOriginalMapper;
import com.quantgroup.data.entity.valobj.RyOriginalDataDTO;
import com.quantgroup.data.service.AttributionDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

/**
 * 归因数据服务
 *
 * @author osmondy
 * @create 2021/10/8 18:23
 */
@Slf4j
@Service
public class AttributionDataServiceImpl extends ServiceImpl<RyOriginalMapper, RyOriginalDataDTO> implements AttributionDataService {

    @Resource
    FirstApolloConfig firstApolloConfig;

    @Override
    public String test() {
        return firstApolloConfig.getEnv();
    }

    /**
     * 保存热云原始数据
     *
     * @param paramMap
     */
    @Override
    public void saveRyOriginalData(String type, Map<String, Object> paramMap) {
        // 转化成热云原始对象
        RyOriginalDataDTO ryOriginalDataDTO = Convert.convert(RyOriginalDataDTO.class, paramMap);
        // 处理数据
        ryOriginalDataDTO.setDataType(type);

        try {
            String ryAdgroupName = ryOriginalDataDTO.getRy_adgroup_name();
            if (StrUtil.isNotEmpty(ryAdgroupName)) {
                String decode = URLDecoder.decode(ryAdgroupName, "UTF-8");
                ryOriginalDataDTO.setRy_adgroup_name(decode);
            }
            String ryAdplanName = ryOriginalDataDTO.getRy_adplan_name();
            if (StrUtil.isNotEmpty(ryAdplanName)) {
                String decode = URLDecoder.decode(ryAdplanName, "UTF-8");
                ryOriginalDataDTO.setRy_adplan_name(decode);
            }
            String ryAdcreativeName = ryOriginalDataDTO.getRy_adcreative_name();
            if (StrUtil.isNotEmpty(ryAdcreativeName)) {
                String decode = URLDecoder.decode(ryAdcreativeName, "UTF-8");
                ryOriginalDataDTO.setRy_adcreative_name(decode);
            }
        } catch (UnsupportedEncodingException e) {
            log.error("----", e);
        }

        this.save(ryOriginalDataDTO);
    }


}
